package org.ggumm21.moneybook;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Id;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Parent;

public class AccountItem extends AbstractDataRecord {

	@Id private Long id;
	private Date itemDate;
	private long cateId;
	private String desc;
//	private double amt;
	private String ym;
	private boolean isMinus;
	@SuppressWarnings("unused")
	@Transient private long cateTypeId; // Just for bean method getCateTypeId
	List<String> balances;
	List<Double> amts;
	
	@JsonIgnore
	@Parent 
	private Key<AccountCategoryType> cateTypeKey;
	
	private AccountCategoryType.CATE_TYPE cateType;
	
	@SuppressWarnings("unused")
	private AccountItem() {
	}
	
	public AccountItem(Date date, AccountCategory cate, double[] amts, String desc) {
		this.itemDate = date;
		this.ym = BookManager.getYm(date);
		this.cateId = cate.getId();
		this.cateTypeKey = new Key<AccountCategoryType>(AccountCategoryType.class, cate.getCateTypeId());
		this.desc = (desc==null?"":desc);	
		this.isMinus = cate.isMinus();
		this.cateType = cate.getCateType();
		this.amts = new ArrayList<Double>();
		this.balances = new ArrayList<String>();
		for(int i = 0; i < amts.length; i++) {
			if (amts[i] == 0 || Double.isNaN(amts[i])) continue;
			this.amts.add(amts[i]);
			this.balances.add(BookManager.getBalanceName(i));
		}
	}
	
	public long getId() {
		return id;
	}
	
	public Date getItemDate() {
		return itemDate;
	}
	
	public String getDesc() {
		return desc;
	}
	
	public double getAmt() {
		if (amts == null) return 0;
		double amtS = 0;
		for(Double amt : amts) {
			amtS += amt;
		}
		return amtS;
	}
	
	public long getCategoryId() {
		return cateId;
	}
	
	public AccountCategoryType.CATE_TYPE getCateType() {
		return cateType;
	}
	
	public long getCateTypeId() {
		return cateTypeKey.getId();
	}
	
	public void setItemDate(Date itemDate) {
		this.itemDate = itemDate;
		this.ym = BookManager.getYm(itemDate);
	}
	
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	public void setAmt(double []amts) {
		if (this.amts == null) {
			this.amts = new ArrayList<Double>();
			this.balances = new ArrayList<String>();
		}
		else {
			this.amts.clear();
			this.balances.clear();
		}
		for(int i = 0; i < amts.length; i++) {
			this.amts.add(amts[i]);
			this.balances.add(BookManager.getBalanceName(i));
		}
	}
	
	public void setCategoryId(long cateId) {
		this.cateId = cateId;
	}

	public boolean isMinus() {
		return isMinus;
	}
	
	public String getYm() {
		return ym;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(this.getClass().getName())
			.append(" : id=").append(id)
			.append(", ym=").append(ym)
			.append(", itemDate=").append(itemDate)
			.append(", cate=[").append(cateType.getName())
				.append("|").append(cateId).append("]")
			.append(", desc=[").append(desc).append("]");
		if (amts == null) {
			sb.append(", amts=null");
		} else {
			for(int i = 0; i < amts.size(); i++) {
				sb.append(", " + balances.get(i) + "=").append(amts.get(i));
			}
		}
		return sb.toString();
	}
	
	public Map<String, Double> getAmtDetails() {
		Map<String, Double> map = new HashMap<String, Double>();
		for(int i = 0; i < amts.size(); i++) {
			map.put(balances.get(i), amts.get(i));
		}
		return map;
	}
	
	public double getAmtDetail(String balName) {
		if (amts == null) return 0;
		for(int i = 0; i < amts.size(); i++) {
			if (balances.get(i).equals(balName))
				return amts.get(i);
		}
		return 0;
	}
}
